package se.ramfelt.psn.web;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.SSLContext;

import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.params.HttpParams;

/**
 * A Socket factory that overrides all security when it comes to SSL. Use this
 * only for testing!
 */
public class AcceptAnyCertificateSocketFactory implements SocketFactory {
    public boolean isSecure(Socket arg0) throws IllegalArgumentException {
        return true;
    }

    public Socket createSocket() throws IOException {
        try {
            return SSLContext.getInstance("SSL").getSocketFactory().createSocket();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public Socket connectSocket(Socket arg0, String arg1, int arg2, InetAddress arg3, int arg4, HttpParams arg5) throws IOException, UnknownHostException,
            ConnectTimeoutException {
        return SSLSocketFactory.getSocketFactory().connectSocket(arg0, arg1, arg2, arg3, arg4, arg5);
    }
}